﻿using AutoMapper;
using BaseAPI.BaseModel;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json;
using Snowflake.Core;

namespace BaseAPI.BaseViewModel;

/// <summary>
/// 创建实体基类，默认自动映射属性（可覆盖CreateModel方法）
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
public abstract class BaseCreateViewModel<T> where T : BaseEntity
{
    /// <summary>
    /// 该项不需要填写
    /// </summary>
    [JsonIgnore]
    [BindNever]
    public long ID { get; set; }
        
    /// <summary>
    /// 使用雪花ID覆盖原ID
    /// </summary>
    /// <param name="mapper"></param>
    /// <param name="idWorker"></param>
    /// <returns></returns>
    public virtual T CreateModel(IMapper mapper, IdWorker idWorker)
    {
        ID = idWorker.NextId();
        return mapper.Map<T>(this);
    }
}